<template>
  <div style="padding: 10px">
    <!--    上传区域-->
    <div style="margin: 10px 0">
      <el-upload
          v-loading.fullscreen.lock="uploading"
          action="/api/minio/upload"
          :on-progress="handleUpload"
          :on-success="handleUploadSuccess"
          :show-file-list=false
          :limit="1"
          >
        <el-button type="primary">上传</el-button>
      </el-upload>
    </div>
    <!--    搜索区域-->
    <div style="margin: 10px 0">
      <el-input v-model="search" placeholder="请输入关键字" style="width: 20%" clearable></el-input>
      <el-button type="primary" style="margin-left: 5px" @click="getFileList">查询</el-button>
    </div>

    <!--    文件列表-->
    <el-table
        v-loading="loading"
        :data="tableData"
        border
        stripe
        style="width: 100%">
      <el-table-column
          label="文件名"
          width="400">
        <template #default="scope">
          <!--          <i class="el-icon-view el-icon&#45;&#45;right"></i>-->
          <el-link v-bind:href="scope.row.url" type="primary" :underline="true" style="color: #409EFF">
            {{ scope.row.fileName }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column
          prop="fileSize"
          label="文件大小">
      </el-table-column>
      <el-table-column
          prop="fileDate"
          label="上传时间">
      </el-table-column>
      <!--      操作列-->
      <el-table-column label="操作" width="400">
        <template #default="scope">
          <el-button size="mini" type="primary" @click="handleDownload(scope.row.fileName)">下载</el-button>

          <el-popconfirm title="确定删除吗？" @confirm="handleDelete(scope.row.fileName)">
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--    分页区域-->
    <div style="margin: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

  </div>

</template>

<script>
import request from "@/utils/request";

export default {
  name: "File",

  data() {
    return {
      loading: true,
      uploading: false,
      tableData: [],
      search: '',
      currentPage: 1,
      pageSize: 10,
      total: 0,
    }
  },
  created() {
    this.getFileList()
  },
  methods: {
    handleUpload() {
      this.uploading = true
      // this.uploading = this.$loading({
      //   lock: true,
      //   text: '拼命上传中',
      //   spinner: 'el-icon-loading',
      //   background: 'rgba(0, 0, 0, 0.7)'
      // });
    },
    handleUploadSuccess(res) {
      this.uploading = false;
      if (res.code === "0") {
        this.$message.success("上传成功")
        this.getFileList()
      }else {
        this.$message.error("上传失败："+res.msg)
      }
      setTimeout(() => {
        location.reload()
      }, 1500);
    },
    getFileList() {
      this.loading = true
      request.get("/minio/file/list", {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          fileName: this.search
        }
      }).then(res => {
        this.loading = false
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    handleDownload(fileName) {
      location.href = "http://localhost:8014/minio/download/" + fileName;
    },
    handleDelete(fileName) {
      request.post("/minio/delete/" + fileName).then(res => {
        if (res.code === "0") {
          this.$message.success("删除成功")
          this.getFileList()
        }
      })
    },
    handleSizeChange(pageSize) {   // 改变当前每页的个数触发
      this.pageSize = pageSize
      this.getFileList()
    },
    handleCurrentChange(pageNum) {  // 改变当前页码触发
      this.currentPage = pageNum
      this.getFileList()
    }
  }
}
</script>

<style scoped>

</style>